const {
    EditorData
} = require('./editor-data');

const {
    JSONModel
} = require('../../../../../public/json-helper/JSONModel');

const {
    JMC_VERSION
} = require('./config');

class JMCData extends JSONModel {

    static className() {
        return 'JMCData';
    }

    constructor() {
        super();
        this._version = JMC_VERSION;
    }

    static singlePageJMCData() {
        let jmcData = new JMCData();
        let editorData = new EditorData();
        jmcData.data.push(editorData);
        return jmcData;
    }

    /**
     * 通过json字符串实例化
     * @param JSONString  目标json字符串
     * @returns {*}
     */
    static initWithJSONString(JSONString) {
        return this.initWithJSONObject(JSON.parse(JSONString));
    }

    /**
     * 通过json对象实例化
     * @param JSONObject  目标json对象
     * @returns {*}
     */
    static initWithJSONObject(JSONObject) {
        let jmcData = new this();
        if (JSONObject) {
            jmcData.originalVersion = JSONObject._version;
            // console.log(JSONObject);
            if (JSONObject.version === 1) {
                if (Array.isNotEmpty(JSONObject.oHangPuBiao)) {
                    JSONObject.oHangPuBiao.forEach((hangPuBiao, i) => {
                        let editorData = EditorData.initWithJSONObject_v1(hangPuBiao);
                        editorData.backgroundColor = JSONObject.color[i];
                        jmcData.data.push(editorData);
                    });
                }
            } else if (Array.isNotEmpty(JSONObject._data)) {
                JSONObject._data.forEach((editorDataJSONObject) => {
                    let editorData = EditorData.objectFromJSONObject(editorDataJSONObject, JSONObject);
                    jmcData.data.push(editorData);
                });
            }
        }
        return jmcData;
    }

    get isUnsaved() {
        for (let i = 0; i < this.data.length; i++) {
            if (this.data[i].history.canUndo() && !this.data[i].history.isSaved) return true;
        }
        return false;
    }

    get data() {
        if (!this._data) {
            this._data = [];
        }
        return this._data;
    }

    set data(value) {
        this._data = value;
    }

    get originalVersion() {
        return this._originalVersion;
    }

    set originalVersion(value) {
        this._originalVersion = value;
    }

    get version() {
        return this._version;
    }

    set version(value) {
        this._version = value;
    }

    static JSONStringifyFilter(key, value) {
        if ((value && ((value.className && value.className.includes('UIYinFu')) || (value.constructor && value.constructor.className && value.constructor.className() && value.constructor.className().includes('UIYinFu'))))
            || key === '_fuGangArray' || key === '_jianShiXianArray' || key === '_zengShiXianArray' || key === '_yinJi' || key === '_yinZu' || key === '_history' || key === '_tmpId' || key === '_from') {
            return;
        }
        return value;
    }

    save(path) {
        // let jmcSaveData = {};
        // jmcSaveData._data = [];
        // this.data.forEach((editorData => {
        //     let obj = {};
        //     obj._sketchpadData = editorData._sketchpadData;
        //     obj._musicScoreData = editorData._musicScoreData;
        //     obj._thumbnailBase64Data = editorData._thumbnailBase64Data;
        //     jmcSaveData._data.push(obj);
        // }));
        JSONModel.save(path, JSONModel.JSONString(this, this.constructor.JSONStringifyFilter));
    }
}

JMCData.VERSION = JMC_VERSION;

module.exports = {
    JMCData
};
